package com.ms.hotevent.service;

import com.ms.hotevent.dao.EventDao;
import com.ms.hotevent.entity.Event;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class EventService {

    @Autowired
    EventDao eventDao;

    public List<Event> query(Integer pageIndex, Integer pageRows, Event event, String sort, String order){
        if(sort == null || sort.equals("")){
            sort = "id";
            order = "desc";
        }
        List<Event> events = eventDao.query(pageIndex,pageRows,event,sort,order);
        return events;
    }

    //返回总页数
    public int queryPagesCount(Integer pageRows,Event event){
        //总行数
        int rowsCount = eventDao.queryRowsCount(event);
        //转换成总页数
        int pageCount = (rowsCount%pageRows==0)?rowsCount/pageRows:rowsCount/pageRows+1;

        return pageCount;
    }

    public Event getOne(Integer id){
        return eventDao.getOne(id);
    }

    public void add(Event event){
        event.setCreateTime(new Date());
        eventDao.add(event);
    }

    public void updateSearchIndex(int eventId,int increase){
        Event event = eventDao.getOne(eventId);
        event.setSearchIndex(event.getSearchIndex()+increase);
        eventDao.updateSearchIndex(event);
    }
}
